package com.qmerak.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qmerak.dao.impl.OrgDao;
import com.qmerak.dao.impl.UnitDao;
import com.qmerak.entity.MemberEntity;
import com.qmerak.entity.OrgEntity;
import com.qmerak.entity.PostUnitEntity;
import com.qmerak.service.AbstractService;
import com.qmerak.service.IOrgService;

/**
 * @author shaohong.cheng
 * @date 2013-4-11
 */
@Service
public class OrgServiceImpl extends AbstractService<OrgEntity> implements
		IOrgService {
	@Autowired
	private OrgDao orgDao;
	@Autowired
	private UnitDao unitDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.service.IOrgService#add(com.entity.OrgEntity)
	 */
	@Override
	public OrgEntity add(OrgEntity entity) {
		return orgDao.add(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.service.IOrgService#delete(com.entity.OrgEntity)
	 */
	@Override
	public void delete(OrgEntity entity) {
		orgDao.remove(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.service.IOrgService#update(com.entity.OrgEntity)
	 */
	@Override
	public void update(OrgEntity entity) {
		orgDao.update(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.service.IOrgService#list()
	 */
	@Override
	public List<OrgEntity> list() {
		return orgDao.list();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.service.IOrgService#listByManager(com.entity.MemberEntity)
	 */
	@Override
	public List<OrgEntity> listByManager(MemberEntity memberEntity) {
		List<OrgEntity> list = new ArrayList<OrgEntity>();
		OrgEntity orgEntity = orgOfManager(memberEntity);
		list.add(orgEntity);
		List<OrgEntity> lowOrgs = orgDao.getLowLevelOrg(orgEntity);
		for (OrgEntity oEntity : lowOrgs) {
			list.addAll(orgDao.getLowLevelOrg(oEntity));
		}
		list.addAll(lowOrgs);
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.service.IOrgService#orgOfManager(com.entity.MemberEntity)
	 */
	@Override
	public OrgEntity orgOfManager(MemberEntity memberEntity) {
		PostUnitEntity unitEntity = unitDao.find(memberEntity.getWorkUnit());
		return orgDao.find(unitEntity.getUnionID());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.qmerak.service.IOrgService#children(com.qmerak.entity.OrgEntity)
	 */
	@Override
	public List<OrgEntity> children(OrgEntity parentOrgEntity) {
		return orgDao.getLowLevelOrg(parentOrgEntity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.qmerak.service.IOrgService#rootOrg()
	 */
	@Override
	public List<OrgEntity> rootOrgs() {
		return orgDao.getRootOrgs();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.qmerak.service.IOrgService#find(java.lang.String)
	 */
	@Override
	public OrgEntity find(String id) {
		return orgDao.find(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.qmerak.service.IOrgService#findByCondition(java.lang.String)
	 */
	@Override
	public List<OrgEntity> findByCondition(String condition) {
		return orgDao.queryByCondition(condition);
	}
}
